package com.lsw.jgyf.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.lsw.Base.AdapterRefresh;
import com.lsw.dialog.DialogInterface;
import com.lsw.dialog.PubDialogUtil;
import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.MyOrderAdapter;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.model.OrderAuditModel;
import com.lsw.jgyf.model.OrderModel;
import com.lsw.jgyf.ui.activity.OrderDetailsActivity;
import com.lsw.jgyf.ui.base.fragment.BaseFragment;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/12 0012.
 */

public class MyOrderFragment4 extends BaseFragment<MyOrderFragmentPresenter> implements MyOrderFragmentContract.View, PullToRefreshLayout.OnRefreshListener, AdapterRefresh {
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    @BindView(R.id.pullableListView)
    PullableListView pullableListView;
    MyOrderAdapter myOrderAdapter;
    String search = "", status = "3";
    PubDialogUtil dialogUtil;
    int page = Constants.PAGE_START;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_myorder1;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        pullToRefreshLayout.setOnRefreshListener(this);
        pullableListView.setCanPulldown(true);
        myOrderAdapter = new MyOrderAdapter(mContext);
        pullableListView.setAdapter(myOrderAdapter);
        pullableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, OrderDetailsActivity.class);
                intent.putExtra("id", myOrderAdapter.getList().get(position).getId() + "");
                startActivity(intent);
            }
        });
    }

    @Override
    protected void lazyLoadData() {
        mPresenter.getData(search, status, page + "");
    }

    public void seach(String seach) {
        this.search = seach;
        if (mPresenter != null) {
            page = Constants.PAGE_START;
            mPresenter.getData(search, status, page + "");
        }

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = Constants.PAGE_START;
        mPresenter.getData(search, status, page + "");
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getData(search, status, ++page + "");
    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }

    @Override
    public void setData(OrderModel model, boolean ispage) {
        pullableListView.setCanPullup(ispage);
        if (page == 1)
            myOrderAdapter.setData(model.getData());
        else myOrderAdapter.addData(model.getData());
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        if (taskid == 1) {
            dialogUtil.showDialogtitleList("请选择审核失败原因", "极果优分", new DialogInterface() {
                @Override
                public void setOk(String data) {
                    dialogUtil.dismiss();
                    String deny_type;
                    if (TextUtils.equals("现金购买", data)) {
                        deny_type = "1";
                    } else {
                        deny_type = "2";
                    }
                    mPresenter.AuditFailure(myOrderAdapter.getList().get(position).getId() + "", deny_type);
                }
            }, false, "");
        } else if (taskid == 2) {
            ToastUtil.showToast(mContext, "审核成功！");
            mPresenter.AuditSuccess(myOrderAdapter.getList().get(position).getId() + "");
        }
    }

    @Override
    public void AuditFailure(OrderAuditModel model) {

    }

    @Override
    public void AuditSuccess(OrderAuditModel model) {

    }
}
